import CryptoJS from 'crypto-js'
import { JSEncrypt } from 'jsencrypt'

// export default {
/**
 * @param {*需要加密的字符串 注：对象转化为json字符串再加密} word
 * @param {*aes加密需要的key值，这个key值后端同学会告诉你} keyStr
 */

export function rsaEncryp(data) {
    let encryption = new JSEncrypt()
    encryption.setPublicKey('MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAoQh0wEqx/R2H1v00IU12Oc30fosRC/frhH89L6G+fzeaqI19MYQhEPMU13wpeqRONCUta+2iC1sgCNQ9qGGf19yGdZUfueaB1Nu9rdueQKXgVurGHJ+5N71UFm+OP1XcnFUCK4wT5d7ZIifXxuqLehP9Ts6sNjhVfa+yU+VjF5HoIe69OJEPo7OxRZcRTe17khc93Ic+PfyqswQJJlY/bgpcLJQnM+QuHmxNtF7/FpAx9YEQsShsGpVo7JaKgLo+s6AFoJ4QldQKir2vbN9vcKRbG3piElPilWDpjXQkOJZhUloh/jd7QrKFimZFldJ1r6Q59QYUyGKZARUe0KZpMQIDAQAB')
    let newData = encryption.encrypt(data)
    return newData
}

export function randomNumber() {
    return (('0000000000000000' + Math.floor(Math.random() * 9999999999999999)).slice(-16));
}

export function encrypt(word, keyStr) { // 加密

    var key = CryptoJS.enc.Utf8.parse(keyStr)

    var srcs = CryptoJS.enc.Utf8.parse(word)

    var encrypted = CryptoJS.AES.encrypt(srcs, key, { mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7 }) // 加密模式为ECB，补码方式为PKCS5Padding（也就是PKCS7）

    return encrypted.toString()

}

//     decrypt(word, keyStr) { // 解密

//         var key = CryptoJS.enc.Utf8.parse(keyStr)

//         var decrypt = CryptoJS.AES.decrypt(word, key, { mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7 })

//         return CryptoJS.enc.Utf8.stringify(decrypt).toString()

//     }

// }